#include "hexvalidator.hpp"

HexValidator::HexValidator(QObject *parent) : QValidator(parent)
{

}

QValidator::State HexValidator::validate(QString &input, int &pos) const
{
    if(!input.isEmpty()) {
        // TODO: can we detect if the char which was JUST deleted
        // (if any was deleted) was a space? and special case this?
        // as to not have the bug in this case?

        const int char_pos  = pos - input.left(pos).count(' ');
        int chars           = 0;
        fixup(input);

        pos = 0;

        while(chars != char_pos) {
            if(input[pos] != ' ') {
                ++chars;
            }
            ++pos;
        }

        // favor the right side of a space
        if(input[pos] == ' ') {
            ++pos;
        }
    }
    return QValidator::Acceptable;

}

void HexValidator::fixup(QString &input) const
{
    QString temp;
    int index = 0;
    input = input.toUpper ();
    QString baseHex = "0123456789ABCDEF";
    Q_FOREACH(QChar ch, input) {
        if(baseHex.contains (ch)) {
            if(index != 0 && (index & 1) == 0) {
                temp += ' ';
            }
            temp += ch.toUpper();
            ++index;
        }
    }
    input = temp;
}
